0#Mucks up a dataSet like a Boss.
import string
import random
import copy
from util import *

def mucker(dataSet, numIterations):
    #see muckEmAll(numIterations) for example usage
    for _ in range(numIterations):
        for _ in range(2):
            addConstantFeatures(dataSet)#looping this twice accomplishes the adding identicalFeatures
        addMissingFeatures(dataSet)
        addCaseWithMissingValues(dataSet)
        addIdenticalCases(dataSet)
        addInconsistantCases(dataSet)
    #writeToCSV(dataSet, numIterations)
    return dataSet

#def addIdeneticalFeatures(dataSet):
def addConstantFeatures(dataSet):
    #Fills an entire row (at random) with a constant value which is randomly selected
    col = random.randrange(len(dataSet.features))
    row = random.randrange(len(dataSet.instances))
    newValue = dataSet.instances[row].features[col].value
    for item in dataSet.instances:
        item.features[col].value = newValue

def addMissingFeatures(dataSet):
#Removes a random feature value from the dataSet
    newValue = '?'
    col = random.randrange(len(dataSet.features) -1)
    row = random.randrange(len(dataSet.instances))
    dataSet.instances[row].features[col].value = newValue

def addIdenticalCases(dataSet):
    #Selects a case (row) at random, copies it, and pastes it at the end of the file
    index  = random.randrange(len(dataSet.instances))
    temp   = dataSet.instances[index]
    dataSet.instances.append(temp)

def addInconsistantCases(dataSet):
    #Selects a case (row) at random, copies it but swaps the class variable, and pastes it at the end of the file
    index  = random.randrange(len(dataSet.instances))
    temp   = copy.deepcopy(dataSet.instances[index])
    end    = len(dataSet.features) -1
    if temp.features[end].value == '0':
        temp.features[end].value = 1
    else:
        temp.features[end].value = 0
    dataSet.instances.append(temp)

def addCaseWithMissingValues(dataSet):
    #Adds at least 2 missing values to a case in order to fulfill the problem definition
    newValue = '?'
    col = random.randrange(len(dataSet.features)-2)
    row = random.randrange(len(dataSet.instances))
    dataSet.instances[row].features[col].value = newValue
    if dataSet.instances[row].features[col].id == len(dataSet.features)-1: 
        dataSet.instances[row].features[col-1].value = newValue
    else:
        dataSet.instances[row].features[col+1].value = newValue
        
def muckEmAll(numIterations = 5): # 1 ~ 5-10TPF, 3 ~ 10-15TPF, 5 ~ 15-24TPF 
    itemIndex = 0                 # varies based on how the dataSet is 
    for item in dataSetList:      # structured 23-24TPF is almost always
        tempds = loadDataSet(item)# 100% of the features.
        tempds.id = itemIndex
        #generateCounterReport(tempds)
        mucker(tempds, numIterations)
        itemIndex += 1


##Testing Area
#ds = loadDataSet(dataSetList[1])
#muckEmAll()
#mucker(ds, 1)
#generateCounterReport(ds)
#printDataSet(ds)


